package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.hbpa.dao.StudentJtDao;
import com.hbpa.form.StudentJtItem;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;
 
public class StudentJtDaoImpl implements StudentJtDao {

	public boolean addStudentJt(String xh, String brgx, String xm, String dw,
			String zw, String tel) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_jt(xh,brgx,xm,dw,zw,tel) values(?,?,?,?,?,?)");
			ps.setString(1, xh);
			ps.setString(2, brgx); 
			ps.setString(3, xm);
			ps.setString(4, dw); 
			ps.setString(5, zw); 
			ps.setString(6, tel); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkStudentJt(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_jt where xh = ?");
			
			ps.setString(1, xh);
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(!count.equals("0")){
					return true;
				}else{
					return false;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public boolean deleteAllStudentJt(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jt where xh=?");
			ps.setString(1,xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean deleteStudentJt(String id, String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jt where id=? and xh=?");
			ps.setString(1, id);
			ps.setString(2, xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public List<StudentJtItem> getStudentJtByXh(String xh) {
		if(checkStudentJt(xh)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		List<StudentJtItem> studentJtItems = new ArrayList<StudentJtItem>();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_jt where xh = ? order by id asc");

			ps.setString(1, xh);
			rs = ps.executeQuery();
			while(rs.next())
			{   
				StudentJtItem item = new StudentJtItem();
				item.setId(rs.getString("id"));
				item.setXh(rs.getString("xh"));
				item.setBrgx(rs.getString("brgx"));
				item.setXm(rs.getString("xm"));
				item.setDw(rs.getString("dw")); 
				item.setZw(rs.getString("zw"));
				item.setTel(rs.getString("tel"));
				studentJtItems.add(item);
			}
			 return studentJtItems;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}
 
}
